package com.frxuexi.task.view

import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material3.ElevatedCard
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import androidx.paging.compose.items
import com.frxuexi.lib_base.MyTitleBar
import com.frxuexi.lib_base.RouterPath
import com.frxuexi.lib_base.UiState
import com.frxuexi.lib_base.room.Task
import com.frxuexi.task.R
import com.frxuexi.task.intent.TaskIntent
import com.frxuexi.task.viewmodel.TaskViewModel
import com.google.gson.Gson
import kotlinx.coroutines.launch

@Composable
fun TaskListPage(navHostController: NavHostController,vm:TaskViewModel= hiltViewModel()) {

    var list= remember {
        SnapshotStateList<Task>()
    }
    LaunchedEffect(key1 = "", block = {
        launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*>->{
                        list.clear()
                        Log.i("===","获取列表：${it.data}")
                        list.addAll(it.data as Collection<Task>)
                    }
                    else->{}
                }
            }
        }


        vm.sendIntent(TaskIntent.getTasks)
    })

    Column {
        MyTitleBar(title = "任务列表", navHostController = navHostController)

        Row(modifier = Modifier
            .padding(horizontal = 20.dp)
            .background(colorResource(id = R.color.lightblue))
            .padding(horizontal = 20.dp, vertical = 15.dp)
            .fillMaxWidth(),horizontalArrangement = Arrangement.SpaceAround) {
            Text(text = "产品经理")
            Text(text = "任务名称")
            Text(text = "提交时间")
            Text(text = "审批状态")
        }
        Spacer(modifier = Modifier.height(10.dp))
        LazyColumn(modifier = Modifier
            .padding(horizontal = 20.dp)
            .fillMaxWidth(), verticalArrangement = Arrangement.spacedBy(10.dp), content = {
            items(list){
                ElevatedCard(modifier = Modifier.clickable {
                    val json = Gson().toJson(it)
                    navHostController.navigate(RouterPath.TASKDETAIL_PAGE+"/$json")
                }) {
                    Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.SpaceAround,modifier = Modifier
                        .padding(horizontal = 20.dp, vertical = 15.dp)
                        .fillMaxWidth()) {
                        Text(text = it?.uname!!)
                        Text(text = it.name!!)
                        Text(text = it.time!!)
                        val color: Color =if(it.status=="已提交") Color.Gray else if(it.status=="同意") Color.Green else Color.Red
                        Text(text = it.status!!,
                            Modifier
                                .background(color, RoundedCornerShape(10.dp))
                                .padding(horizontal = 10.dp, vertical = 3.dp))
                    }
                }

            }
        })
    }
}